#include "includes.h"
#include "smbd/smbd.h"

static NTSTATUS smb_authenticate(struct auth_context *actx,
                                TALLOC_CTX *mem_ctx,
                                const struct auth_usersupplied_info *user_info)
{
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        char url[512];
        user_info->
        snprintf(url, sizeof(url), "http://localhost:7713/auth?userName=%s&password=%s", user_info->client.account_name,user_info->client.newpassword);

        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "Samba HTTP Auth Plugin");
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            DEBUG(0,("curl_easy_perform() failed: %s\n", curl_easy_strerror(res)));
            return NT_STATUS_ACCESS_DENIED;
        }

        long http_code = 0;
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
        if(http_code != 200) {
            DEBUG(0,("Authentication server returned error code %ld\n", http_code));
            return NT_STATUS_ACCESS_DENIED;
        }
    }

    curl_easy_cleanup(curl);
    curl_global_cleanup();

    return NT_STATUS_OK;
}

NTSTATUS auth_init_smbauth(void)
{
    return auth_register(auth_methods, SMB_AUTH_INTERFACE_VERSION, "smbauth",
                         smb_authenticate);
}